package MyDisplayObjects
{
	import avmplus.FLASH10_FLAGS;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.text.*;
	import flash.utils.getQualifiedClassName;
	import flash.utils.getQualifiedSuperclassName;
	
	/** An object of this class  loads an image */
	public class ClickableImage extends ClickableMC
	{
		private var ldr:Loader;
		private var labelTField:TextField = null;
		private var imageName:String;
		private var WIDTH:Number, HEIGHT:Number;
		
		//override public function get width():Number{if (ldr != null){return ldr.width} else {return 0;}} 
		//override public function get height():Number{if (ldr != null){return ldr.height} else {return 0;}} 
		
		public function ClickableImage(width:Number, height:Number, imageName:String, label:String="", fontSize:Number=20, color:uint=0xFFFFFF, autoDisable:Boolean=false)
		{
			super(color, autoDisable);
			this.WIDTH = width;
			this.HEIGHT = height;
			this.imageName = imageName;
			this.graphics.lineStyle(fontSize/10, 0x888888);
			this.graphics.beginFill(color);
			this.graphics.drawRect(0, 0, width, height);
			this.graphics.endFill();
			
			// label textfield
			if (label.length > 0)
			{
				labelTField = new TextField();
				labelTField.selectable = false;
				labelTField.mouseEnabled = false;
				labelTField.defaultTextFormat = new TextFormat("Arial", fontSize, ClickableMC.LINE_COLOR);
				labelTField.text = label;
				labelTField.autoSize = "left";
				labelTField.x = (this.width - labelTField.width)/2;
				labelTField.y = this.height - labelTField.height;
				this.addChild(labelTField);
			}
			
			// load image			
			ldr = new Loader();
			var urlReq:URLRequest = new URLRequest(String("MathTextModeler/images/"+imageName));
			ldr.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, handleLoadedImage);
			ldr.load(urlReq);
			
		}
		private function handleLoaderError (event:IOErrorEvent):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			this.dispatchEvent( new Event(Event.CANCEL));
		}
		/** Once the image has been loaded, display it */
		private function handleLoadedImage (event:Event):void
		{
			ldr.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, handleLoaderError);
			ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleLoadedImage);
			ldr.mouseEnabled = false;
			if (flash.utils.getQualifiedSuperclassName(ldr.content) == "flash.display::MovieClip")
			{
				var mc:MovieClip = ldr.content as MovieClip;
				mc.stop();
			}
			var prop:Number;
			// wider or taller
			// can we scale up?
			if (ldr.width/WIDTH > ldr.height/(HEIGHT-labelTField.height))
			{
				prop = WIDTH/ldr.width
			} else
			{
				prop = (HEIGHT-labelTField.height)/ldr.height;
			}			
			
			ldr.scaleX = prop;
			ldr.scaleY = prop;
			ldr.x = (this.WIDTH - ldr.width)/2;
			ldr.y = 0;
			this.addChild(ldr);			
			
		}
	}
}